<?php
class OnlinesController extends AppController {

	var $name = 'Onlines';
	
	function onlines()
	{
		$this->layout = null;
		$guests = $this->Online->find('all', array('conditions'=>array('Online.offline'=>0)));
		$users = $this->Online->find('all', array('conditions'=>array('Online.offline'=>0)));
		$total = $this->Online->find('count');
		
		//random count
		Controller::loadModel('Random');
		$record = $this->Random->read(null, 1);		
		if(strtotime(date('Y-m-d H:i:s')) - strtotime($record["Random"]["date"]) > 60*60*8)
		{
			$rand = rand(1500, 4500);
			$up = $rand - $record["Random"]["rand"];
			
			$record["Random"]["rand"] = $rand;
			$record["Random"]["date"] = date('Y-m-d H:i:s');
			
			if($up > 0)
			{
				$record["Random"]["total"] += $up;
			}
			$this->Random->save($record);	
		}
		
		if(strtotime(date('Y-m-d H:i:s')) - strtotime($record["Random"]["date_small"]) > 120)
		{
			$down = round(rand(-95, -1)*(rand(75,150)/100), 0);
			$up = round(rand(1, 95)*(rand(75,150)/100), 0);
			
			if($record["Random"]["rand"] > 4500)
			{
				$up = 0;
			}
			else if($record["Random"]["rand"] < 1500)
			{
				$down = 0;
			}
			
			$rand = $record["Random"]["rand"] + rand($down, $up);
			$up = $rand - $record["Random"]["rand"];
			
			$record["Random"]["rand"] = $rand;
			$record["Random"]["date_small"] = date('Y-m-d H:i:s');
			
			if($up > 0)
			{
				$record["Random"]["total"] += $up;
			}
			$this->Random->save($record);	
		}
		
		
		$count["guest"] = count($guests) + $record["Random"]["rand"];
		$count["user"] = count($users);
		$count["total"] = $total + $record["Random"]["total"];
		$this->set('count', $count);
	}
	
	function delArticles()
	{
		$this->layout = null;
		Controller::loadModel('Content');
		
		
		
		
		//THANHNIEN
		$contents = $this->Content->find('all', array('conditions'=>array(
								'introtext LIKE'=>'%Theo Thanh Niên%'
							)));
		
		
		foreach($contents as $content)
		{
			$order   = array("\r\n", "\n", "\r");
			$replace = '';
			
			// Processes \r\n's first so they aren't converted twice.
			$newstr = str_replace($order, $replace, $content['Content']['introtext']);
			
			//find last p tag
			preg_match_all( '/\<p(.*?)\>(.*?)\<\/p\>/', $newstr, $match_all );//var_dump($match_all);
			//var_dump($match_all);
			//$reference_link = $match_all[0][count($match_all[0])-1];
			
			$ii = count($match_all[0])-1;
			$min = count($match_all[0])-10;
			while(trim(strip_tags($match_all[0][$ii])) == "" && $ii > $min)
			{
				$ii--;
			}
			$reference_link = $match_all[0][$ii];
			echo $content['Content']['title'];
			echo $reference_link."<br />";
			if(preg_match( '/(.*?)(thanh niên|thanh nien)(.*?)/' , strtolower($reference_link) ))
			{
				$content['Content']['state'] = "0";
				$this->Content->save($content);
				echo "ok";
			}
			
			
			//$newstr = str_replace(array($img_tag[0], $match_mota[7], $reference_link), '', $newstr);
		}
		
		
		
		
		
		
		////VIETNAMNET
		$contents = $this->Content->find('all', array('conditions'=>array(
								'introtext LIKE'=>'%Theo Vietnamnet%'
							)));
		
		
		foreach($contents as $content)
		{
			$order   = array("\r\n", "\n", "\r");
			$replace = '';
			
			// Processes \r\n's first so they aren't converted twice.
			$newstr = str_replace($order, $replace, $content['Content']['introtext']);
			
			//find last p tag
			preg_match_all( '/\<p(.*?)\>(.*?)\<\/p\>/', $newstr, $match_all );//var_dump($match_all);
			//var_dump($match_all);
			//$reference_link = $match_all[0][count($match_all[0])-1];
			
			$ii = count($match_all[0])-1;
			$min = count($match_all[0])-10;
			while(trim(strip_tags($match_all[0][$ii])) == "" && $ii > $min)
			{
				$ii--;
			}
			$reference_link = $match_all[0][$ii];
			echo $content['Content']['title'];
			echo $reference_link."<br />";
			if(preg_match( '/(.*?)vietnamnet(.*?)/' , strtolower($reference_link) ))
			{
				$content['Content']['state'] = "0";
				$this->Content->save($content);
				echo "ok<br />";
			}
			
			
			//$newstr = str_replace(array($img_tag[0], $match_mota[7], $reference_link), '', $newstr);
		}
		
		
		
		//VEF
		$contents = $this->Content->find('all', array('conditions'=>array(
								'introtext LIKE'=>'%Theo VEF%'
							)));
		
		
		foreach($contents as $content)
		{
			$order   = array("\r\n", "\n", "\r");
			$replace = '';
			
			// Processes \r\n's first so they aren't converted twice.
			$newstr = str_replace($order, $replace, $content['Content']['introtext']);
			
			//find last p tag
			preg_match_all( '/\<p(.*?)\>(.*?)\<\/p\>/', $newstr, $match_all );//var_dump($match_all);
			//var_dump($match_all);
			//$reference_link = $match_all[0][count($match_all[0])-1];
			
			$ii = count($match_all[0])-1;
			$min = count($match_all[0])-10;
			while(trim(strip_tags($match_all[0][$ii])) == "" && $ii > $min)
			{
				$ii--;
			}
			$reference_link = $match_all[0][$ii];
			echo $content['Content']['title'];
			echo $reference_link."<br />";
			if(preg_match( '/(.*?)vef(.*?)/' , strtolower($reference_link) ))
			{
				$content['Content']['state'] = "0";
				$this->Content->save($content);
				echo "ok<br />";
			}
			
			
			//$newstr = str_replace(array($img_tag[0], $match_mota[7], $reference_link), '', $newstr);
		}
		
		
		
		//THEO TTVN
		$contents = $this->Content->find('all', array('conditions'=>array(
								'introtext LIKE'=>'%Theo TTVN%'
							)));
		
		
		foreach($contents as $content)
		{
			$order   = array("\r\n", "\n", "\r");
			$replace = '';
			
			// Processes \r\n's first so they aren't converted twice.
			$newstr = str_replace($order, $replace, $content['Content']['introtext']);
			
			//find last p tag
			preg_match_all( '/\<p(.*?)\>(.*?)\<\/p\>/', $newstr, $match_all );//var_dump($match_all);
			//var_dump($match_all);
			//$reference_link = $match_all[0][count($match_all[0])-1];
			
			$ii = count($match_all[0])-1;
			$min = count($match_all[0])-10;
			while(trim(strip_tags($match_all[0][$ii])) == "" && $ii > $min)
			{
				$ii--;
			}
			$reference_link = $match_all[0][$ii];
			echo $content['Content']['title'];
			echo $reference_link."<br />";
			if(preg_match( '/(.*?)ttvn(.*?)/' , strtolower($reference_link) ))
			{
				$content['Content']['state'] = "0";
				$this->Content->save($content);
				echo "ok<br />";
			}
			
			
			//$newstr = str_replace(array($img_tag[0], $match_mota[7], $reference_link), '', $newstr);
		}
		
		
		//var_dump($contents);
	}
	
	function addUser()
	{
		$this->layout = null;
		Controller::loadModel('Content');
		
		//$contents = $this->Content->find('all', array('conditions'=>array(
		//						'created_by'=>'49'
		//					)));
		//
		//foreach($contents as $content)
		//{
		//	$content['Content']['created_by'] = "45";
		//	$this->Content->save($content);
		//	echo $content['Content']['title']."-".$content['Content']['catid']."<br />";
		//}
		
		//$contents = $this->Content->find('all', array('conditions'=>array(
		//						'created_by'=>'46'
		//					)));
		//
		//foreach($contents as $content)
		//{
		//	$content['Content']['created_by'] = "45";
		//	$this->Content->save($content);
		//	echo $content['Content']['title']."-".$content['Content']['catid']."<br />";
		//}
		
		$contents = $this->Content->find('all', array('conditions'=>array(
								'created_by'=>'85'
							)));
		
		foreach($contents as $content)
		{
			$content['Content']['created_by'] = "70";
			$this->Content->save($content);
			echo $content['Content']['title']."-".$content['Content']['catid']."<br />";
		}
	}
	
	function get_img_style()
	{
		$this->layout = null;
		$img = $_POST["img"];
		$top = $_POST["top"];
		$k_width = $_POST["width"];
		$k_height = $_POST["height"];
		
		list($width, $height, $type, $attr) = getimagesize($img);
		
		if($width && $height)
		{
			$zoom = $width/$k_width;
			if($height/$k_height > $zoom)
			{
				$amount = (($height/$zoom) - $k_height)*$top;
				//echo $amount;
				echo "clip: rect(".$amount."px, ".$k_width."px, ".($k_height+$amount)."px, 0px);width:".$k_width."px !important;height: auto;position: absolute;min-height:".$k_height."px !important;max-height:none !important;margin-top:-".$amount."px";
			}
		}
		
		//echo $width."/".$height;
		//if($width != 0) $amount = 0;
		    //$amount = (($height - ($width/2))/2)*(625/$width);
		//echo $amount;
		//$style = "clip: rect(".$amount."px, 625px, ".(350+$amount)."px, 0px);min-width:625px;max-width:625px;max-height:600px;min-height:350px;margin-top:-".$amount."px";
	}

	function index() {
		//random count
		Controller::loadModel('Random');
		$record = $this->Random->read(null, 1);
		//echo $record["Random"]["total"];
		
		$this->Online->recursive = 0;
		$this->layout = null;
		$guests = $this->Online->find('all', array('conditions'=>array('Online.offline'=>0)));
		$users = $this->Online->find('all', array('conditions'=>array('Online.offline'=>0)));
		$total = $this->Online->find('count');
		$count["guest"] = count($guests);
		$count["user"] = count($users);
		$count["total"] = $total + $record["Random"]["total"];
		$this->set('onlines', $this->paginate());
		$this->set('count', $count);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid online', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('online', $this->Online->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Online->create();
			if ($this->Online->save($this->data)) {
				$this->Session->setFlash(__('The online has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The online could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid online', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Online->save($this->data)) {
				$this->Session->setFlash(__('The online has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The online could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Online->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for online', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Online->delete($id)) {
			$this->Session->setFlash(__('Online deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Online was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
